package com.sky.controller.user;

import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.admin.AddressBookService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class AddressBookController {
    private final AddressBookService addressBookService;
    /*
    * 新增地址
    * */
    @PostMapping("/addressBook")
    public Result saveAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.saveAddressBook(addressBook);
        return Result.success();
    }

    /*
    * 查询登录用户所有的地址
    * */
    @GetMapping("/addressBook/list")
    public Result selectAddressBoorList(){
        List<AddressBook> addressBookList = addressBookService.selectAddressBoorList();
        return Result.success(addressBookList);
    }

    /*
    * 查询默认地址
    * */
    @GetMapping("/addressBook/default")
    public Result selectAddressBookDefault(){
        AddressBook addressBook= addressBookService.selectAddressBookDefault();
        return Result.success(addressBook);
    }
    /*
     * 修改地址
     * */
    @PutMapping("/addressBook")
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }
    /*
    * 根据id删除地址
    * */
    @DeleteMapping("/addressBook")
    public Result deleteAddressBookById(Long id){
        addressBookService.deleteAddressBookById(id);
        return Result.success();
    }
    /*
    * 根据路径id查询地址
    * */
    @GetMapping("/addressBook/{id}")
    public Result selectAddressBookById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.selectAddressBookById(id);
        return Result.success(addressBook);
    }
    /*
    * 根据传入的地址id设置默认地址
    * */
    @PutMapping("/addressBook/ault")
    public Result setAddressBookDefault(@RequestBody AddressBook addressBook){
        Long id = addressBook.getId();
        addressBookService.setAddressBookDefault(id);
        return Result.success();
    }
}
